package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.DealJsonString;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.BatchDoLmsAnswerTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 408698 信息技术与教育技术(1)
 */
@Slf4j
public class BatchDoLmsAnswertmp {


    public static void main(String[] args) throws Exception {



        int sync = 4;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();

        InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//all3.user");
        List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1];

            Map<Long, String> courseMap = LmsUtil.getUserCourseMap(userCode);
            if (null == courseMap) {
                continue;
            }

            for (Long courseId : courseMap.keySet()) {

                Map<String, AnswerObject> answerMap = initAnswer(courseMap.get(courseId));
                if (null == answerMap) {
                    continue;
                }
                LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
                lmsWatchDTO.setUsername(userCode);
                lmsWatchDTO.setPassword(pwd);
                lmsWatchDTO.setAnswer(answerMap);
                lmsWatchDTO.setCourseId(courseId);
                lmsWatchDTO.setCourseName(courseMap.get(courseId));
                log.info("lmsWatchDTO:{},{},{},{}", userCode, pwd, courseId, courseMap.get(courseId));
                watchQueue.add(lmsWatchDTO);
            }


        }

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new BatchDoLmsAnswerTask("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }
    }


    private static Map<String, AnswerObject> initAnswer(String subjectName) throws IOException {
//        if (
//                !subjectName.contains("思想道德与法治")
//                        && !subjectName.contains("习近平新时代中国特色社会主义思想")
//                        && !subjectName.contains("思想道德修养与法律基础")
//                        && !subjectName.contains("形势与政策")
//                        && !subjectName.contains("毛泽东思想和中国特色社会主义理论体系概论")
//                        && !subjectName.contains("中国近现代史纲要")
//                        && !subjectName.contains("马克思主义基本原理概论")
//                        && !subjectName.contains("国家开放大学学习指南")
//                        && !subjectName.contains("计算机应用基础")
//                        && !subjectName.contains("金融学")
//        ) {
//            return null;
//        }
//        if(!subjectName.contains("西方行政制度")){
//            return null;
//        }
        File file = new File("D:\\project\\answer-tool\\src\\main\\resources\\" + subjectName + "\\" + subjectName + "-answer.json");
        if (!file.exists()) {
            return null;
        }
        InputStream inputStream = new FileInputStream(file);
        String answerStr = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
        Map<String, AnswerObject> answerMap = Maps.newHashMap();

        boolean isEngCourse;
        isEngCourse = subjectName.contains("英语");
        for (AnswerObject answerObject : list) {
            answerMap.put(answerObject.getQuestionTitle(), answerObject);
            if (isEngCourse) {
                answerMap.put(DealJsonString.removeSpace(answerObject.getQuestionTitle()), answerObject);
            } else {
                answerMap.put(DealJsonString.saveZhWord(answerObject.getQuestionTitle()), answerObject);
                answerMap.put(Util.dealContent(answerObject.getQuestionTitle()), answerObject);
                answerMap.put(DealJsonString.delHTMLTag(Util.dealContent(answerObject.getQuestionTitle())), answerObject);
            }
        }

        return answerMap;
    }

}
